<template>
  <!-- 账户管理 -->
  <div class="account-container">
    <PageCard
      title="查询条件"
      :showTitle="false"
    >
      <el-form
        :model="formData"
        label-position="right"
        :inline="true"
        size="small"
        ref="searchForm"
      >
        <el-form-item prop="name">
          <el-input
            class="w350"
            placeholder="请输入单位名称"
            v-model="formData.name"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            class="w350"
            placeholder="请输入医疗机构编码"
            v-model="formData.code"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSearch()"
          >查询</el-button>
          <el-button @click="handleReset()">重置</el-button>
        </el-form-item>

      </el-form>

    </PageCard>
    <PageCard
      title="医院机构"
      icon_type="list"
      :hasCreate="true"
      btnText="新增医院机构"
      @create="handleAdd()"
    >
      <PageTable
        :data="data"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        @handleChange="handleSearch"
      >
        <el-table-column
          prop="name"
          label="单位名称"
          width="160"
        >
        </el-table-column>
        <el-table-column
          prop="code"
          label="医疗机构编码"
        > </el-table-column>
        <el-table-column
          prop="creditCode"
          label="企业信用代码"
        > </el-table-column>
        <el-table-column
          prop="address"
          width="200"
          show-overflow-tooltip
          label="地址"
        >
        </el-table-column>
        <el-table-column
          prop="adminName"
          label="管理员"
        >
        </el-table-column>
        <el-table-column
          prop="adminMobile"
          label="管理员电话"
        >
        </el-table-column>

        <el-table-column
          label="操作"
          width="230"
        >
          <template slot-scope="scope">
            <!-- <el-button
              type="text"
              @click.stop="handleRole(scope.row)"
            >角色分配</el-button> -->
            <el-button
              type="text"
              @click.stop="handleEdit('edit',scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              class="text-warning"
              @click.stop="handleEdit('view',scope.row)"
            >查看</el-button>
            <el-button
              type="text"
              :class="{'text-danger':scope.row.isFreeze == 0,'text-success':scope.row.isFreeze == 1}"
              @click.stop="handleDel(scope.row)"
            >
              <!-- 1 禁用  0 启用 -->
              {{scope.row.isFreeze == 1 ? '启用' : '禁用'}}
            </el-button>
          </template>
        </el-table-column>
      </PageTable>
    </PageCard>
    <Child
      ref="child"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></Child>
    <ChooseRole
      ref="chooseRole"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></ChooseRole>
  </div>
</template>

<script>
import PageTable from "@/components/PageTable";
import PageCard from "@/components/PageCard";
import child from "./components/child.vue";
import chooseRole from "./components/chooseRole.vue";
import { roleList } from "@/api/setting1.js";
import * as hospitalApi from "@/api/hospital";
import * as department from "@/api/department.js";
import * as areaApi from "@/api/area";
import { settings } from "nprogress";
export default {
  name: "Users",
  components: {
    PageTable: PageTable,
    PageCard: PageCard,
    Child: child,
    ChooseRole: chooseRole,
  },
  data() {
    return {
      props: {
        children: "children",
        checkStrictly: true,
        lazy: true,
        async lazyLoad(node, resolve) {
          const { level } = node;
          if (level == 0) {
            //省
            const { data } = await areaApi.getProvinceList();
            var nodes = data.map((item) => {
              return {
                value: item.code,
                label: item.name,
                leaf: false,
              };
            });
            resolve(nodes);
          } else if (level == 1) {
            const { data } = await areaApi.getCityList({
              provinceCode: node.data.value,
            }); //获取市接口
            var nodes = data.map((item) => {
              return {
                value: item.code,
                label: item.name,
                leaf: false,
              };
            });
            resolve(nodes);
          } else if (level == 2) {
            const { data } = await areaApi.getRegionList({
              cityCode: node.data.value,
            }); //获取市接口
            var nodes = data.map((item) => {
              return {
                value: item.code,
                label: item.name,
                leaf: false,
              };
            });
            resolve(nodes);
          } else if (level == 3) {
            const { data } = await areaApi.getTownList({
              regionCode: node.data.value,
            }); //获取市接口
            var nodes = data.map((item) => {
              return {
                value: item.code,
                label: item.name,
                leaf: true,
              };
            });
            resolve(nodes);
          }
        },
      },
      formData: {
        showAll:true,
        name: "",
        roleId: "",
        addressCode: [],
        code: "",
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      data: [],
      roleList: [], //角色列表
      multipleTable: [], //批量选择
    };
  },
  created() {
    this.handleSearch(1, 10);
    this.handleGetRoleList();
  },
  methods: {
    // 查询
    handleSearch(current, limit) {
      this.currentPage = current;
      this.pageSize = limit;
      const params = {
        ...this.formData,
        pageIndex: current || 1,
        pageSize: limit || 10,
      };
      // 调接口查询
      this.handleGetDataList(params);
    },
    //获取角色列表
    handleGetRoleList() {
      roleList({ isPage: false }).then((res) => {
        if (res && res.success) {
          this.roleList = res.data;
        }
      });
    },
    //获取列表数据
    handleGetDataList(params) {
      hospitalApi.hospitalGetList(params).then((res) => {
        if (res && res.success) {
          this.data = res.data;
          this.total = res.total;
        }
      });
    },
    //获取省市区数据
    handleLocationsChange(val) {
      this.formData.addressCode = JSON.parse(JSON.stringify(val));
      this.formData.provinceCode = this.formData.addressCode[0] || "";
      this.formData.cityCode = this.formData.addressCode[1] || "";
      this.formData.regionCode = this.formData.addressCode[2] || "";
      this.formData.townCode = this.formData.addressCode[3] || "";
      this.formData = Object.assign({}, this.formData);
    },
    // 重置
    handleReset() {
      this.$refs["searchForm"].resetFields();
      this.formData.provinceCode = "";
      this.formData.cityCode = "";
      this.formData.regionCode = "";
      this.formData.townCode = "";
    },
    handleAdd() {
      this.$refs.child.showModal("add");
    },
    handleEdit(type, row) {
      this.$refs.child.showModal(type, row);
    },
    //下载二维码
    handleGenerateQrCode(row) {
      department.generateQrCode(row.id).then((res) => {
        const link = document.createElement("a");
        const blob = new Blob([res], {
          type: "image/png", // "application/zip",
        });
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        link.download = row.realName; // 下载后文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },

    //删除用户
    handleDel(item) {
      this.$confirm(
        `此操作将${item.isFreeze == 1 ? "启用" : "禁用"}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          hospitalApi
            .hospitalBaseSave({
              id: item.id,
              isFreeze: item.isFreeze == 0 ? 1 : 0,
            })
            .then((res) => {
              if (res && res.success) {
                this.handleSearch(this.currentPage, this.pageSize);
                this.$message({
                  type: "success",
                  message: `${item.isFreeze == 1 ? "启用" : "禁用"}成功`,
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分配角色
    handleRole(row) {
      this.$refs.chooseRole.showModal("edit", row);
    },
    //重置密码
    handleResetPwd() {},
    //多选
    selectionChange(val) {
      this.multipleTable = val;
    },
  },
};
</script>

<style lang="scss" scoped>
.account-container {
  .btns {
    text-align: right;
  }
}
</style>
